package com.example.exam.controller;

import com.alibaba.excel.EasyExcel;
import com.example.exam.ExcelListener.AddTeacherListener;
import com.example.exam.entity.Roles;
import com.example.exam.entity.User;
import com.example.exam.entity.temp.TempStudent;
import com.example.exam.entity.temp.TempTeacher;
import com.example.exam.service.RolesServiceImpl;
import com.example.exam.service.UserServiceImpl;
import com.example.exam.service.admin.AdminService;
import com.example.exam.utils.FileUtils;
import com.example.exam.utils.MailUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Objects;

/**
 * @author zhang
 */
@Log4j2
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private MailUtils mail;
    @Autowired
    private AddTeacherListener addTeacherListener;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private RolesServiceImpl rolesService;
    //创建管理员账户
    @GetMapping("/create")
    @Transactional(rollbackFor = Exception.class)
    public String create() {
        Integer n = adminService.create();
        mail.sendMail("2158003625@qq.com", "创建管理员账户", "创建管理员账户成功");
        if (n == 1) {
            return "success";
        }
        return "failed";
    }
    //批量添加教师账户
    @PostMapping("/addTeacher")
    public ResponseEntity<String> addTeacher(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("文件为空");
        }
        if (!FileUtils.isExcel(Objects.requireNonNull(file.getOriginalFilename()))) {
            return ResponseEntity.badRequest().body("文件类型错误");
        }
        InputStream inputStream;
        try {
            inputStream = file.getInputStream();
            EasyExcel.read(inputStream, TempTeacher.class, addTeacherListener).sheet().doRead();
            inputStream.close();
        } catch (Exception e) {
            log.error("发生了错误:[]", e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
        return ResponseEntity.ok("success");
    }

    //批量添加学生账户
    @PostMapping("/addStudent")
    public ResponseEntity<String> addStudent(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("文件为空");
        }
        if (!FileUtils.isExcel(Objects.requireNonNull(file.getOriginalFilename()))) {
            return ResponseEntity.badRequest().body("文件类型错误");
        }
        InputStream inputStream;
        try {
            inputStream = file.getInputStream();
            EasyExcel.read(inputStream, TempStudent.class, addTeacherListener).sheet().doRead();
            inputStream.close();
        } catch (Exception e) {
            log.error("发生了错误:[]", e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
        return ResponseEntity.ok("success");
    }

    @PostMapping("/addOneTeacher")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<String> addOne(@RequestBody TempTeacher tempTeacher, @RequestParam Integer role) {
        User user = new User();
        Roles roles = new Roles();
        BeanUtils.copyProperties(tempTeacher, user);
        user.userInit();
        log.info("user:{}", user);
        if (userService.save(user)) {
            roles.setRoles(role);
            roles.setId(user.getId());
            if (rolesService.save(roles)) {
                return ResponseEntity.ok("success");
            }
        }
        return ResponseEntity.badRequest().body("failed");
    }
}
